package SEApp.boxes.templates
{
	import SEApp.boxes.albums.se_abox;
	import SEApp.boxes.albums.se_previewItem;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_hscrollArea;
	import UIFree.controls.uif_hscrollAreaA;
	import UIFree.controls.uif_tabItem;
	import UIFree.uif_component;
	
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class se_templates extends se_abox {
		private var _groups			: Array;
		private var _templates		: Array;
		private var _displayCount	: uint;
		
		public static const TEMPLATESELECT: String = 'TEMPLATESELECT';
		
		public function se_templates(parent:uif_component, a_groups:Array, a_templates: Array,a_displayCount: uint=9, ax:int=0, ay:int=0, 
									awidth:int=0, aheight:int=0, atabwidth:int=0, styleClass:String='') {
			super(parent, a_groups, ax, ay, awidth, aheight, atabwidth, styleClass);
			_itemsLayer.compareFunc = function(item1: Object, item2: Object): Boolean {
				return item1 && item2 && (item1.id == item2.id);
			};
			_displayCount 		= a_displayCount;
			_groups 			= a_groups;
			_templates 			= a_templates;
			tabs.selectIndex 	= 0;
//			selectTemplateFromID(defaultID);
		}
		

/*		
		public function selectTemplateFromID(templateID: uint): void {
			for each (var tmpl: Object in _templates)
				if (tmpl.id == templateID) {
					tabs.selectIndex = tmpl.gid;
					for (var i:int=0; i<_itemsLayer.items.length; i++)
						if (_itemsLayer.items[i].object && (_itemsLayer.items[i].object.id == templateID)) {
							_itemsLayer.selectIndex = i;
							break; 
						}
				}
		}
*/		
		
		override protected function createItemsLayer(inner: Rectangle): uif_hscrollArea {
			return new uif_hscrollAreaA(layer, inner.x, inner.y, inner.width, inner.height, style['itemslayer_class'], 27, true);
		}
		
		override protected function createItem(list: Array, inner: Rectangle, index: int, styleName: String): uif_tabItem {
			inner.inflate(9, 0);
			inner.x += 9;
			var item: uif_tabItem = new uif_tabItem(tabs.scrollArea, inner, styleName);
			item.text = list[index].name;
			return item; 
		}
		
		protected function doItemClick(e: MouseEvent): void {
			if ((e.target as se_templateItem).data) dispatchEvent(new ObjectEvent(TEMPLATESELECT, (e.target as se_templateItem).data, true, true));
		}
		
		protected function assignData(data: Array, itemClass: Class): void {
			var inner: Rectangle = _itemsLayer.scrollArea.getInner();
			while (data.length < _displayCount) data.push(null);
			data.sortOn('id', Array.NUMERIC | Array.DESCENDING);
			
			_itemsLayer.assignList(data, _itemsLayer.calcFrameWidth(2), function(_scroll: uif_component, inner: Rectangle, index: int): se_previewItem {
				var item: se_templateItem =  new itemClass(_scroll, inner.x, inner.y, inner.width, inner.height, data[index], _itemsLayer.style['unselect']);
				item.addEvent(MouseEvent.CLICK, doItemClick);
				return item;
			}, 2);
		}
		
		override protected function doChangeSelect(tabIndex: int): void {
			function filter(elem:*, index:int, arr:Array):Boolean {
				return elem.gid == _groups[tabIndex].id;
			}
			assignData(_templates.filter(filter), se_templateItem);
		}		
	}
}